import datetime
import multiprocessing
from util import *
import copy
from CloudSystem import run_once

break_flage=False

def generate_default_args():
    args=Args()
    args.deploy_schedule_strategy = "ours"
    args.request_num=1
    args.spec_reliability=0.9999
    args.min_lambda_transient_ms=0.0000001
    args.max_lambda_transient_ms=0.00001
    # args.trace_id
    args.print_level=0
    
    return args


def run_once_this(heuristic_method, jump_request_num):
    global RR_cost, ours_cost
    print(f"start once {heuristic_method}, {trace_id}")
    args=generate_default_args()
    args.heuristic_method=heuristic_method
    args.jump_request_num=jump_request_num
    args_copy=copy.deepcopy(args)
    ave_replica_num, ave_response_time=run_once(args_copy)
    
    print(f"End once {heuristic_method}, {trace_id}")
    return (heuristic_method, trace_id, ave_replica_num)
    
    
trace_id=24

while not break_flage:
    
    task_args_list=[]

    for method in ["down_relia", "up_deal_time",]:#,"R_RIR", "C_GM", "QFEC", "GSMS"
        task_args_list.append((method, trace_id))
            
                    

    with multiprocessing.Pool(processes=1) as pool:
        results = pool.starmap(run_once_this, task_args_list)
        
    # 结构化结果
    cost_dict = {method: ave_replica_num for method, tid, ave_replica_num in results if tid == trace_id}
    
    if cost_dict["down_relia"]-cost_dict["up_deal_time"] >0.000000001:
        print(f"up_deal_time cost:{cost_dict['up_deal_time']}, down_relia cost:{cost_dict['down_relia']}, trace-ID:{trace_id}")
        break
    else:
        print(f"up_deal_time cost:{cost_dict['up_deal_time']}, down_relia cost:{cost_dict['down_relia']}, trace-ID:{trace_id}")
        trace_id += 1

